/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package YottaTron.Map;

import java.io.*;
import java.util.*;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author Cenda
 */
public class Map implements Serializable{
public int numObjects;
public int backgroundGraphics;
public String backgroundName;
public ArrayList<Obstacle> obstacles;
public int width, height , tileWidth, tileHeight;
public int[][] gameField;

    public Map(String backgroundName, int backgroundGraphics, int width, int height, int tileWidth, int tileHeight) {
        this.backgroundName = backgroundName;
        this.backgroundGraphics = backgroundGraphics;
        obstacles = new ArrayList<Obstacle>();
        this.width = width;
        this.height = height;
        this.tileWidth = tileWidth;
        this.tileHeight = tileHeight;

        gameField = new int[width / tileWidth][height / tileHeight];
        //inicializace herniho pole
        for (int i = 0; i < gameField.length; i++) {
            for (int j = 0; j < gameField[0].length; j++) {
                gameField[i][j] = 0;
            }
        }
    }

    public Map(String fileName){
        load(fileName);
    }

    public Obstacle getObstacleByID(int ID) {
        Obstacle o = null;
        for (Iterator iter = obstacles.iterator(); iter.hasNext();) {
            o = (Obstacle) iter.next();
            if (o.ID == ID) {
                return o;
            }
        }
        return null;
    }

        public void removeObstacleByID(int ID) {
        Obstacle o = null;
        for (Iterator iter = obstacles.iterator(); iter.hasNext();) {
            o = (Obstacle) iter.next();
            if (o.ID == ID) {
                obstacles.remove(o);
                return;
            }
        }
    }


    public void save(String fileName) {
        FileOutputStream fos = null;
        ObjectOutputStream oos = null;
        try {
            fos = new FileOutputStream("src/maps/" + fileName);  // FileOutputStream(jmeno,true) pripisuje na konec souboru
            oos = new ObjectOutputStream(fos);
            oos.writeObject(backgroundName);
            oos.writeInt(backgroundGraphics);
            oos.writeInt(width);
            oos.writeInt(height);
            oos.writeInt(tileWidth);
            oos.writeInt(tileHeight);
            oos.writeObject(obstacles);
            for (int i = 0; i < gameField.length; i++) {
                for (int j = 0; j < gameField[0].length; j++) {
                    oos.writeInt(gameField[i][j]);
                }
            }
            oos.flush();
            oos.close();

        } catch (IOException ex) {
            Logger.getLogger(Map.class.getName()).log(Level.SEVERE, null, ex);
        } finally {
            try {
                oos.close();
                fos.close();
            } catch (IOException ex) {
                Logger.getLogger(Map.class.getName()).log(Level.SEVERE, null, ex);
            }
        }

    }

    public void load(String fileName) {
        InputStream fis = null;
        ObjectInputStream ois = null;
        try {
            fis = new FileInputStream("src/maps/" + fileName); //c.getResourceAsStream(fileName);
            ois = new ObjectInputStream(fis);

            backgroundName = (String)ois.readObject();
            backgroundGraphics = ois.readInt();
            width = ois.readInt();
            height = ois.readInt();
            tileWidth = ois.readInt();
            tileHeight = ois.readInt();
            obstacles = (ArrayList<Obstacle>)ois.readObject();

            gameField = new int[width / tileWidth][height / tileHeight];
            for (int i = 0; i < gameField.length; i++) {
                for (int j = 0; j < gameField[0].length; j++) {
                    gameField[i][j] = ois.readInt();
                }
            }

        } catch (Exception ex) {
            Logger.getLogger(Map.class.getName()).log(Level.SEVERE, null, ex);
        } finally {
            try {
                fis.close();
                ois.close();
            } catch (IOException ex) {
                Logger.getLogger(Map.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }

}
